package com.firebird.app.kotlin.stock.ui.index_fragment.account

import android.app.Dialog
import android.os.Bundle
import android.support.v4.view.ViewPager
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import butterknife.BindView
import butterknife.OnClick
import butterknife.Unbinder
import com.firebird.app.kotlin.stock.R
import com.firebird.app.kotlin.stock.utils.SharedPreferencesUtil
import com.zabra.zl.Config
import com.zabra.zl.R
import com.zabra.zl.base.BaseMvpFragment
import com.zabra.zl.data.AccountBean
import com.zabra.zl.data.EventMessage
import com.zabra.zl.data.GridData
import com.zabra.zl.ui.about.AboutActivity
import com.zabra.zl.ui.allocation_manage.AllocationManageActivity
import com.zabra.zl.ui.deposit.DepositActivity
import com.zabra.zl.ui.generalize.GeneralizeActivity
import com.zabra.zl.ui.index_fragment.account.contract.AccountFragmentContract
import com.zabra.zl.ui.index_fragment.account.presenter.AccountFragmentPresenter
import com.zabra.zl.ui.nullpage.NullPersonActivity
import com.zabra.zl.ui.personal_center.PersonalActivity
import com.zabra.zl.ui.recharge.RechargeActivity
import com.zabra.zl.ui.runing.RunningActivity
import com.zabra.zl.utils.DialogUtils
import com.zabra.zl.utils.HideUtil
import com.zabra.zl.utils.MaptoJsonUtils
import com.zabra.zl.utils.SharedPreferencesUtil
import com.zabra.zl.utils.ShowDialogUtils
import com.zabra.zl.utils.ToastUtil
import org.greenrobot.eventbus.EventBus
import java.util.*


class AccountFragment : BaseMvpFragment<AccountFragmentPresenter?>(), AccountFragmentContract.View {
    @BindView(R.id.tv_account)
    var tvAccount: TextView? = null
    @BindView(R.id.tv_all)
    var tvAll: TextView? = null
    @BindView(R.id.tv_security)
    var tvSecurity: TextView? = null
    @BindView(R.id.tv_balance)
    var tvBalance: TextView? = null
    @BindView(R.id.ll_recharge)
    var llRecharge: LinearLayout? = null
    @BindView(R.id.ll_deposit)
    var llDeposit: LinearLayout? = null
    @BindView(R.id.viewpager)
    var viewpager: ViewPager? = null
    @BindView(R.id.ll_dot)
    var llDot: LinearLayout? = null
    @BindView(R.id.icon1)
    var icon1: ImageView? = null
    @BindView(R.id.rl_running)
    var rlRunning: RelativeLayout? = null
    @BindView(R.id.icon2)
    var icon2: ImageView? = null
    @BindView(R.id.rl_manage)
    var rlManage: RelativeLayout? = null
    @BindView(R.id.icon3)
    var icon3: ImageView? = null
    @BindView(R.id.rl_generalize)
    var rlGeneralize: RelativeLayout? = null
    @BindView(R.id.icon4)
    var icon4: ImageView? = null
    @BindView(R.id.rl_about)
    var rlAbout: RelativeLayout? = null
    var unbinder: Unbinder? = null
    private val mLlDot: LinearLayout? = null
    private var inflater: LayoutInflater? = null
    private var mPagerList: MutableList<View>? = null
    private var mData: MutableList<GridData>? = null
    private var views: Array<View?>
    private var layoutParams: LinearLayout.LayoutParams? = null
    private val bundle = Bundle()
    private var view: View? = null
    private var dialog: Dialog? = null
    private var realName = 0
    private var strBlance: String? = null
    /**
     * 总的页数
     */
    private var pageCount = 0
    /**
     * 每一页显示的个数
     */
    private val pageSize = 3
    /**
     * 当前显示的是第几页
     */
    private val curIndex = 0

    protected fun initView(view: View?) {
        mPresenter = AccountFragmentPresenter()
        mPresenter.attachView(this)
        keyList.add("debtorId")
        valueList.add(strUserid)
        mPresenter.getData(MaptoJsonUtils.Simple(keyList, valueList))
    }

    protected val layoutId: Int
        protected get() = R.layout.fragment_account

    fun showLoading() {
        dialog = DialogUtils.showDialog_progressbar(getActivity())
    }

    fun hideLoading() {
        DialogUtils.closeDialog(dialog)
    }

    fun onError(throwable: Throwable?) {
        DialogUtils.closeDialog(dialog)
    }

    fun onSuccess(bean: AccountBean, token: String?) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            SharedPreferencesUtil.getInstance(getActivity())!!.putSP("token", token)
            SharedPreferencesUtil.getInstance(getActivity()).putSP("tel", bean.getTel())
            strBlance = bean.getCashBalance()
            //1实名，0未实名
            realName = bean.getAttestation()
            tvAccount!!.text = "账户 " + HideUtil.hideMobile(bean.getTel())
            tvAll.setText(bean.getTotBalance())
            tvSecurity.setText(bean.getSecurityBalance())
            tvBalance!!.text = strBlance
            initDatas(bean)
            inflater = LayoutInflater.from(getActivity())
            //总的页数=总数/每页数量，并取整
            pageCount = Math.ceil(mData!!.size * 1.0 / pageSize).toInt()
            mPagerList = ArrayList()
            for (i in 0 until pageCount) { // 每个页面都是inflate出一个新实例
                val gridView = View.inflate(getActivity(), R.layout.gridview, null) as GridView
                gridView.adapter = GridViewAdapter(getActivity(), mData, i, pageSize)
                //                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                        int pos = position + curIndex * pageSize;
//
//                    }
//                });
                mPagerList.add(gridView)
            }
            viewpager!!.adapter = ViewPagerAdapter(mPagerList)
            //            setOvalLayout();
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(getActivity())
        } else {
            ToastUtil.showToast(getActivity(), bean.getRespDesc())
        }
    }

    @OnClick(R.id.tv_account, R.id.ll_recharge, R.id.ll_deposit, R.id.rl_running, R.id.rl_manage, R.id.rl_generalize, R.id.rl_about)
    fun onViewClicked(view: View) {
        when (view.id) {
            R.id.tv_account ->  //账户信息
                toClass1(getActivity(), PersonalActivity::class.java)
            R.id.ll_recharge -> {
                //充值
                bundle.putString("blance", strBlance)
                toClass(getActivity(), RechargeActivity::class.java, bundle)
            }
            R.id.ll_deposit ->  //提现
                if (realName == 0) {
                    val message = EventMessage(1, "Main")
                    EventBus.getDefault().postSticky(message)
                    toClass1(getActivity(), NullPersonActivity::class.java)
                } else {
                    toClass(getActivity(), DepositActivity::class.java)
                }
            R.id.rl_running ->  //流水
                toClass(getActivity(), RunningActivity::class.java)
            R.id.rl_manage ->  //管理
                if (realName == 0) {
                    val message = EventMessage(1, "Main")
                    EventBus.getDefault().postSticky(message)
                    toClass1(getActivity(), NullPersonActivity::class.java)
                } else {
                    toClass(getActivity(), AllocationManageActivity::class.java)
                }
            R.id.rl_generalize ->  //推广
                toClass(getActivity(), GeneralizeActivity::class.java)
            R.id.rl_about ->  //关于
                toClass1(getActivity(), AboutActivity::class.java)
        }
    }

    private fun initDatas(bean: AccountBean) {
        mData = ArrayList<GridData>()
        val data1 = GridData()
        val data2 = GridData()
        val data3 = GridData()
        data1.setName("持仓市值")
        data1.setMoney(bean.getPositionBalance())
        data1.setIcon(R.mipmap.grid1)
        data2.setName("可投余额")
        data2.setMoney(bean.getUseBalance())
        data2.setIcon(R.mipmap.grid2)
        data3.setName("浮动盈亏")
        data3.setMoney(bean.getEarnBalance())
        data3.setIcon(R.mipmap.grid3)
        mData.add(data1)
        mData.add(data2)
        mData.add(data3)
    }

    /**
     * 设置圆点
     */
    fun setOvalLayout() {
        views = arrayOfNulls(pageCount)
        for (i in 0 until pageCount) {
            views[i] = View(getActivity())
            view = View(getActivity())
            layoutParams = LinearLayout.LayoutParams(8, 8)
            if (i == 0) {
                views[i] = view
                views[i]!!.setBackgroundResource(R.drawable.dot_pitch)
            } else {
                views[i] = view
                views[i]!!.setBackgroundResource(R.drawable.dot_nopitch)
            }
            layoutParams!!.leftMargin = 10
            layoutParams!!.rightMargin = 10
            view!!.layoutParams = layoutParams
            views[i]!!.setPadding(8, 8, 8, 8)
            llDot!!.addView(views[i])
        }
        viewpager!!.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(i: Int, v: Float, i1: Int) {}
            override fun onPageSelected(position: Int) {
                for (i in 0 until pageCount) {
                    if (i == position) {
                        views[i]!!.setBackgroundResource(R.drawable.dot_pitch)
                    } else {
                        views[i]!!.setBackgroundResource(R.drawable.dot_nopitch)
                    }
                }
            }

            override fun onPageScrollStateChanged(i: Int) {}
        })
    }
}